package com.sinaapp.kltq.dinosaurweather;


import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;

import com.sinaapp.kltq.data.CityListItem;
import com.sinaapp.kltq.data.WeatherAlarm;
import com.sinaapp.kltq.weathermanager.WeatherManager;

import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by lenovo on 2015/7/16.
 */

/*
WeatherManager wm = new WeatherManager();
DinosaurWeatherAlarm alarm = wm.getAlarmWeather();
getAlarmWeather("北京");
无预警则读取到的alarmTyoe是none
MainActivity.this.getApplicationContext();
 */
public class NoticeService extends Service {
    private static  String content="latest content";
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }



    static Timer timer = null;
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        long period= 2*60*1000; //2小时一个周期

        System.out.println("period:"+period);
        if (null == timer )
        {
            timer = new Timer();
        }
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                WeatherManager wm = new WeatherManager(NoticeService.this.getApplicationContext());
              //  wm.addCityIntoListEnd("武汉");
                //wm.pleaseUpdateThisSir("武汉");
                List<CityListItem> cityList = wm.getCityList();//获得城市管理中的所有城市

                if (!cityList.isEmpty())//城市列表不为空
                {
                    System.out.println("!cityList.isEmpty!");
                    String currentCity = cityList.get(0).city;//获得默认城市名

                    System.out.println(currentCity);
                    WeatherAlarm wa = wm.getAlarmWeather(currentCity);

                    if (wm.pleaseUpdateThisSir(currentCity)) {//更新数据成功

                        System.out.println("wa.alarmType:" + wa.alarmType);
                        if (!wa.alarmType.equals("none")) {
                           // System.out.println(wa.toString());


                            if (!content.equals(wa.alarmDetails))//避免通知重复发送
                            {
                                content = wa.alarmDetails;
                                System.out.println("content:" + content);
                                sendNotice(wa);//发送通知
                            }
                        }
                    }
                }
            }
        },3000, period);
        return START_STICKY;//服务被系统kill后重启的，服务自身原因崩溃是不会起作用
    }


    public void sendNotice(WeatherAlarm wa){

        String sText="恐龙天气贴心小提示";
        String title=wa.alarmText;
        String content=wa.alarmDetails;
        int ic=R.drawable.dinosaur;
        Notification notification=new Notification(ic,sText,System.currentTimeMillis());//设置通知栏的标题

        Intent intent=new Intent(NoticeService.this,showNotice.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//解决传参问题
        String cityName=wa.cityName;
        intent.putExtra("cityName",cityName);

        String alarmText=wa.alarmType+wa.alarmDegree+"预警";
        intent.putExtra("alarmText",alarmText);

        String time=wa.time+"发布";
        intent.putExtra("time",time);

        intent.putExtra("alarmDetails",content);
        PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);//点击通知栏的时候打开的Activity
        notification.setLatestEventInfo(this,title,content,pendingIntent);//设置显示的标题和内容

        notification.flags=Notification.FLAG_AUTO_CANCEL; //点击之后通知栏取消显示

        notification.defaults=Notification.DEFAULT_SOUND;//设置通知的声音

        //获得系统自带的通知管理器
        NotificationManager notificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
       // notificationManager.cancel(10);
        notificationManager.notify(10, notification);//发送通知
        // System.out.println("sendNotice");
    }

}
